var xIn = argument0;
var yIn = argument1;
var tile = argument2;
var module = argument3;

if(xIn < 0 || yIn < 0)
{
    return -1;
}
with(module)
{
    tile.x = module.x + xIn * 100;
    tile.y = module.y + yIn * 100;
    //Destroy the old tile
    if( is_array(tiles) && 
        array_height_2d(self.tiles) >= yIn + 1 && 
        array_length_2d(self.tiles, yIn) >= xIn + 1 && 
        tiles[yIn, xIn] != 0)
    {
        with(tiles[yIn, xIn])
        {
            instance_destroy();
        }
    }
    self.tiles[yIn, xIn] = tile;
    
    //Expand the module to be rectangular
    var maxWidth = 0;
    for (var i = 0; i < array_height_2d(self.tiles); i++)
    {
        if (array_length_2d(self.tiles, i) > maxWidth)
        {
            maxWidth = array_length_2d(self.tiles, i);
        } 
    }
    for (var i = 0; i < array_height_2d(self.tiles); i++)
    {
        if (array_length_2d(self.tiles, i) < maxWidth)
        {
            for (var j = array_length_2d(self.tiles, i); j < maxWidth; j++)
            {
                self.tiles[i, j] = 0;
            }
        }
    }
    /** old code. Edge case breaks this. New row insertion
    for(var iii = 0; iii < array_height_2d(self.tiles); iii++)
    {
        if (array_length_2d(self.tiles, iii) <= xIn)
        {
            for(var jjj = array_length_2d(self.tiles, iii); jjj <= xIn; jjj++)
            {
                self.tiles[iii, jjj] = 0;
            }
        }
    } **/
}